package com.flyco.modules.cms.service.impl;

import com.flyco.modules.cms.entity.CmsImageTag;
import com.flyco.modules.cms.entity.CmsImageTagProduct;
import com.flyco.modules.cms.mapper.CmsImageTagProductMapper;
import com.flyco.modules.cms.mapper.CmsImageTagMapper;
import com.flyco.modules.cms.service.ICmsImageTagService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 活动图标管理
 * @Author: flyco
 * @Date:   2019-12-25
 * @Version: V1.0
 */
@Service
public class CmsImageTagServiceImpl extends ServiceImpl<CmsImageTagMapper, CmsImageTag> implements ICmsImageTagService {

	@Autowired
	private CmsImageTagMapper cmsImageTagMapper;
	@Autowired
	private CmsImageTagProductMapper cmsImageTagProductMapper;
	
	@Override
	@Transactional
	public void saveMain(CmsImageTag cmsImageTag, List<CmsImageTagProduct> cmsImageTagProductList) {
		cmsImageTagMapper.insert(cmsImageTag);
		for(CmsImageTagProduct entity:cmsImageTagProductList) {
			//外键设置
			entity.setImageTagId(cmsImageTag.getId());
			cmsImageTagProductMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void updateMain(CmsImageTag cmsImageTag,List<CmsImageTagProduct> cmsImageTagProductList) {
		cmsImageTagMapper.updateById(cmsImageTag);
		
		//1.先删除子表数据
		cmsImageTagProductMapper.deleteByMainId(cmsImageTag.getId());
		
		//2.子表数据重新插入
		for(CmsImageTagProduct entity:cmsImageTagProductList) {
			//外键设置
			entity.setImageTagId(cmsImageTag.getId());
			cmsImageTagProductMapper.insert(entity);
		}
	}

	@Override
	@Transactional
	public void delMain(Long id) {
		cmsImageTagProductMapper.deleteByMainId(id);
		cmsImageTagMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			cmsImageTagProductMapper.deleteByMainId((Long)id);
			cmsImageTagMapper.deleteById(id);
		}
	}
	
}
